<?php

include_once './app/model/accountModel.php' ;

class userModel extends Model
{
	
	public static function getById ( $id )
	{
		
		$db = &DB::getInstance ( require './config/db_config.php' ) ;
		
		$params = array (
				'id' => array (
						'value' => intval ( $id ),
						'type' => PDO::PARAM_INT
				)
		) ;
			
		$db->query ( 'SELECT * FROM client WHERE cli_id=:id', $params ) ;
			
		$result = $db->fetchAll ( ) ;
		
		if ( count ( $result ) === 0 )
			return null ;
		else
			return new userModel ( $result[0], true ) ;
		
	}
	
	public static function getByLogin ( $login )
	{
		
		$db = &DB::getInstance ( require './config/db_config.php' ) ;
			
		$params = array (
				'login' => array (
						'value' => strval ( $login ),
						'type' => PDO::PARAM_STR
				)
		) ;
		
		$db->query ( 'SELECT * FROM client WHERE cli_login=:login', $params ) ;
			
		$result = $db->fetchAll ( ) ;
		
		if ( count ( $result ) === 0 )
			return null ;
		else
			return new userModel ( $result[0], true ) ;
		
	}
	
	public static function getAll ( )
	{
		
		$db = &DB::getInstance ( require './config/db_config.php' ) ;
		
		$db->query ( 'SELECT * FROM client' ) ;
			
		$result = $db->fetchAll ( ) ;
		
		if ( count ( $result ) === 0 )
			return null ;
		else
		{
			
			$res = array ( ) ;
			
			foreach ( $result as $user )
				$res[] = new userModel ( $user, true ) ;
			
			return $res ;
			
		}
		
	}
	
	public function save ( )
	{
		
		if ( ! isset ( $this->data['cli_prenom'] ) && ! isset ( $this->data['cli_nom'] ) && ! isset ( $this->data['cli_login'] ) && ! isset ( $this->data['cli_password'] ) )
			throw new Exception ( 'Tous les champs du modèle ne sont pas renseignés.' ) ;
		elseif ( ! $this->already_saved )
		{
			
			$params = array (
					'firstname' => array (
							'value' => strval ( $this->data['cli_prenom'] ),
							'type' => PDO::PARAM_STR
					),
					'lastname' => array (
							'value' => strval ( $this->data['cli_nom'] ),
							'type' => PDO::PARAM_STR
					),
					'login' => array (
							'value' => strval ( $this->data['cli_login'] ),
							'type' => PDO::PARAM_STR
					),
					'password' => array (
							'value' => strval ( $this->data['cli_pass'] ),
							'type' => PDO::PARAM_STR
					)
			) ;
				
			$this->db->query ( 'INSERT INTO Client (cli_prenom, cli_nom, cli_login, cli_pass ) VALUES ( :firstname, :lastname, :login, :password )', $params ) ;
			$this->data['cli_id'] = $this->db->lastId ( ) ;
			
			$this->already_saved = true ;
			$this->modified = false ;
			
		}
		else if ( $this->modified )
		{
			
			$params = array (
					'id' => array (
							'value' => intval ( $this->data['cli_id'] ),
							'type' => PDO::PARAM_INT
					),
					'firstname' => array (
							'value' => strval ( $this->data['cli_prenom'] ),
							'type' => PDO::PARAM_STR
					),
					'lastname' => array (
							'value' => strval ( $this->data['cli_nom'] ),
							'type' => PDO::PARAM_STR
					),
					'login' => array (
							'value' => strval ( $this->data['cli_login'] ),
							'type' => PDO::PARAM_STR
					),
					'password' => array (
							'value' => strval ( $this->data['cli_pass'] ),
							'type' => PDO::PARAM_STR
					)
			) ;
			
			$this->db->query ( 'UPDATE Client SET cli_prenom=:firstname, cli_nom=:lastname, cli_login=:login, cli_pass=:password WHERE cli_id=:id', $params ) ;
			
			$this->modified = false ;
			
		}
		
	}
	
	public function delete ( )
	{
		
		if ( $this->already_saved )
		{
			
			$accounts = accountModel::getByOwner( $this->data['cli_id'] ) ;
			
			if ( $accounts != null )
				foreach ( $accounts as $key => $compte )
				{
					
					$compte->delete ( ) ;
					
				}
			
			unset ( $accounts ) ;
			
			$params = array (
					'id' => array (
							'value' => intval ( $this->data['cli_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;
			
			$this->db->query ( 'DELETE FROM Client WHERE cli_id=:id', $params ) ;
			
			$this->already_saved = false ;
			
		}
		
	}
	
	public function getId ( ) { return $this->data['cli_id'] ; }
	public function getFirstName ( ) { return $this->data['cli_prenom'] ; }
	public function getLastName ( ) { return $this->data['cli_nom'] ; }
	public function getLogin ( ) { return $this->data['cli_login'] ; }
	public function getPassword ( ) { return $this->data['cli_pass'] ; }
	
	public function setFirstName ( $arg ) { $this->data['cli_prenom'] = strval ( $arg ) ; $this->modified = true ; }
	public function setLastName ( $arg ) { $this->data['cli_nom'] = strval ( $arg ) ; $this->modified = true ; }
	public function setLogin ( $arg ) { $this->data['cli_login'] = strval ( $arg ) ; $this->modified = true ; }
	public function setPassword ( $arg ) { $this->data['cli_pass'] = strval ( $arg ) ; $this->modified = true ; }
	
	public function getAccounts ( )
	{
		
		if ( ! isset ( $this->data['cli_id'] ) )
			return null ;
		else
			return accountModel::getByOwner ( $this->data['cli_id'] ) ; }
	
	public function addAccounts ( $accounts = array ( ) )
	{
		
		if ( ! isset ( $this->data['cli_id'] ) )
			throw new Exception ( 'Le modèle n\'a pas encore été enregisté un fois et n\'a donc pas d\'id.' ) ;
		elseif ( ! is_array ( $accounts ) )
			throw new Exception ( 'Le paramètre $accounts n\'est pas un tableau.' ) ;
		else
		{
			
			foreach ( $accounts as $account )
			{
				
				if ( $account instanceof accountModel )
				{
					
					$account->setOwner ( $this->data['cli_id'] ) ;
					$account->save ( ) ;
					
				}
				
			}
			
		}
		
	}
	
	public function removeAccounts ( $accounts_to_rm = array ( ) )
	{
		
		if  ( ! is_array ( $accounts_to_rm ) )
			throw new Exception ( 'Le paramètre $accounts_to_rm n\'est pas un tableau.' ) ;
		else
		{
			
			$my_accounts = getAccounts ( ) ;
			
			foreach ( $my_accounts as $my_account )
			{
				
				foreach ( $accounts_to_rm as $account_to_rm )
				{
					
					if ( $my_account == $account_to_rm )
						$my_account->delete ( ) ;
					
				}
				
			}
			
		}
		
	}

}

?>